package com.eric.windows.perference;

import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.RingtonePreference;
import android.widget.Toast;

import com.eric.windows.R;


public class MyPreferenceFragment extends PreferenceFragment {

    private RingtonePreference ringtonePreference;
    private EditTextPreference editTextPreference;
    private ListPreference listPreference;
    private CheckBoxPreference checkBoxPreference;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        initView();
    }

    private void initView() {
        ringtonePreference = (RingtonePreference) findPreference("ring_key");
        editTextPreference = (EditTextPreference) findPreference("edt_key");
        listPreference = (ListPreference) findPreference("list_key");
        checkBoxPreference = (CheckBoxPreference) findPreference("check_key");


        ringtonePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                //返回铃声所在库的地址 String类型  如：content://media/internal/audio/media/32
                String uri = newValue.toString();
                Toast.makeText(getActivity(), ("路径：" + uri), Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        ringtonePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Toast.makeText(getActivity(), "preference.getTitle():" + preference.getTitle(), Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        editTextPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                 //返回确认后的新值 String类型
                Toast.makeText(getActivity(), ((String) newValue), Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                //返回确认后的新值 String类型
                Toast.makeText(getActivity(), ((String) newValue), Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        checkBoxPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                //返回 true  false  boolean类型
                boolean  isSelected = (boolean) newValue;
                if (isSelected){
                    Toast.makeText(getActivity(), "开启", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getActivity(), "关闭", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });
    }

}
